/**
 * Title:        代理数据同步
 * Company:      泛微软件
 */


package checkDetail;

import general.BaseBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;

import weaver.general.Util;



public class HtmlInfoCheck extends BaseBean implements CheckBase  {

    @Override
    public JSONObject checkData(HashMap dataMap) {
        setRecordSet();
        (new BaseBean()).writeLog("HtmlInfoCheck  start");

        List resultList = new ArrayList();
        String result = resultOk;
        String sqlStr = "";
        sqlStr +=" select HtmlLabelInfo.indexid, HtmlLabelInfo.labelname, HtmlLabelInfo.languageid ";
        sqlStr +="   from HtmlLabelInfo ";
        sqlStr +=" inner join (select count(1) cnt ,indexid, languageid from HtmlLabelInfo group by indexid, languageid having count(1) > 1) count ";
        sqlStr +=" on count.indexid = HtmlLabelInfo.indexid and count.languageid = HtmlLabelInfo.languageid ";
        sqlStr +="  where HtmlLabelInfo.indexid < 0 ";
        sqlStr +="    and HtmlLabelInfo.indexid > -100000000 ";
        sqlStr +="  order by HtmlLabelInfo.indexid, HtmlLabelInfo.languageid ";
        recordSet.executeSql(sqlStr);
        
        String idtemp = "";
        //标签内容比较用
        String indexdesctemp = "";
        //标签内容比较用
        String languageidtemp = "";
        //标签内容表示用
        String indexdescshow = "";
        //其他标签内容拼接
        String indexdescCact = "";
        List<String> indexidList = new ArrayList<String>();
        int cnt = 0;
        while(recordSet.next()){
            String labelid = recordSet.getString("indexid");
            String indexdesc = recordSet.getString("labelname");
            String languageid = recordSet.getString("languageid");
            if(cnt == 0){
                indexdescCact = "";
                indexdescshow = indexdesc;
            }
            if(!idtemp.equals(labelid) && indexidList.indexOf(labelid) == -1){
                result = resultNeedFix;
                Map resultMap = new HashMap<String,String>();
                resultMap.put("showMessage", "标签[" + indexdesc + "]存在相同id的标签内容，需要修复。");
                resultMap.put("labelid",labelid);
                resultMap.put("indexdesc",indexdesc);
                resultMap.put("buttonname","数据修复");
                resultList.add(resultMap);
                indexdescshow = indexdesc;
                indexdescCact = "";
                indexidList.add(labelid);
            }
            cnt++;
        }
        (new BaseBean()).writeLog("HtmlInfoCheck  end");
        return formatReturnJsonCheck(result, resultList,this);
    }

    @Override
    public JSONObject fixData(HashMap dataMap) {
        setRecordSet();
        
        if(dataMap!= null && dataMap.get("divid")!= null){
            //根据参数获取列表
            String labelid = (String)dataMap.get("labelid");
            String indexdesc =  (String)dataMap.get("indexdesc");
            String divid =  (String)dataMap.get("divid");
            
            List resultList = new ArrayList();
            String result = "";

            String sqlStr = "";

            sqlStr +=" select HtmlLabelInfo.indexid, HtmlLabelInfo.labelname, HtmlLabelInfo.languageid ";
            sqlStr +="   from HtmlLabelInfo ";
            sqlStr +=" inner join (select count(1) cnt ,indexid, languageid from HtmlLabelInfo group by indexid, languageid having count(1) > 1) count ";
            sqlStr +=" on count.indexid = HtmlLabelInfo.indexid and count.languageid = HtmlLabelInfo.languageid ";
            sqlStr +="  where HtmlLabelInfo.indexid  =" + labelid ;
            sqlStr +="  order by HtmlLabelInfo.indexid, HtmlLabelInfo.languageid ";
            recordSet.executeSql(sqlStr);
            if(recordSet.next()){
                labelid = recordSet.getString("indexid"); 
                if(recordSet.getDBType().equalsIgnoreCase("oracle")){
                    recordSet.executeUpdate("update HtmlLabelInfo set indexid = indexid - 100000000  where indexid = " + labelid + " and languageid ='7' and rownum = 1");
                    recordSet.executeUpdate("update HtmlLabelInfo set indexid = indexid - 100000000  where indexid = " + labelid + " and languageid ='8' and rownum = 1");
                    recordSet.executeUpdate("update HtmlLabelInfo set indexid = indexid - 100000000  where indexid = " + labelid + " and languageid ='9' and rownum = 1");
                }else{
                    recordSet.executeUpdate("update top(1) HtmlLabelInfo set indexid = indexid - 100000000  where indexid = " + labelid + " and languageid ='7' ");
                    recordSet.executeUpdate("update top(1) HtmlLabelInfo set indexid = indexid - 100000000  where indexid = " + labelid + " and languageid ='8' ");
                    recordSet.executeUpdate("update top(1) HtmlLabelInfo set indexid = indexid - 100000000  where indexid = " + labelid + " and languageid ='9' ");
                }

                sqlStr = "";
                sqlStr +=" select HtmlLabelInfo.indexid, HtmlLabelInfo.labelname, HtmlLabelInfo.languageid ";
                sqlStr +="   from HtmlLabelInfo ";
                sqlStr +=" inner join (select count(1) cnt ,indexid, languageid from HtmlLabelInfo group by indexid, languageid having count(1) > 1) count ";
                sqlStr +=" on count.indexid = HtmlLabelInfo.indexid and count.languageid = HtmlLabelInfo.languageid ";
                sqlStr +="  where HtmlLabelInfo.indexid  =" + labelid ;
                sqlStr +="  order by HtmlLabelInfo.indexid, HtmlLabelInfo.languageid ";
                recordSet.executeSql(sqlStr);
                if(recordSet.next()){
                    result = resultNeedFix;
                    Map resultMap = new HashMap<String,String>();
                    resultMap.put("result",resultNeedFix);
                    resultMap.put("fixMessage","数据备份为" + (- 100000000 + Util.getIntValue(labelid)) + ",仍需要继续修复");
                    resultMap.put("divid",divid);
                    resultList.add(resultMap);
                }else{
                    result = resultOk;
                    Map resultMap = new HashMap<String,String>();
                    resultMap.put("result",resultOk);
                    resultMap.put("fixMessage","数据修复完成,数据备份为" + (- 100000000 + Util.getIntValue(labelid)));
                    resultMap.put("divid",divid);
                    resultList.add(resultMap);
                }
            }else{
                result = resultOk;
                Map resultMap = new HashMap<String,String>();
                resultMap.put("result",resultOk);
                resultMap.put("fixMessage","数据已经正常,不需要修复");
                resultMap.put("divid",divid);
                resultList.add(resultMap);
            }
            return formatReturnJsonFix(result, resultList,this);
        }
        /*else{

            List resultList = new ArrayList();
            String result = "";
            String sqlStr = "";
            sqlStr +=" select id,indexdesc from HtmlLabelIndex  ";
            sqlStr +=" where id < 0 and id > -100000000 and exists ( ";
            sqlStr +=" select b.id from HtmlLabelIndex b where b.id = HtmlLabelIndex.id and b.indexdesc <> HtmlLabelIndex.indexdesc) ";
            sqlStr +=" order by id ";
            recordSet.executeSql(sqlStr);
            
            String idtemp = "";
            int cnt = 0;
            String labelid = recordSet.getString("id");
            String indexdesc = recordSet.getString("indexdesc");
            while(recordSet.next()){
                labelid = recordSet.getString("id");
                indexdesc = recordSet.getString("indexdesc");
                if(!idtemp.equals(labelid)){
                    recordSet.executeUpdate("update HtmlLabelIndex set id = id - 100000000  where id = " + labelid + " and indexdesc <> '" + indexdesc + "'");
                    idtemp = labelid;
                }
                cnt++;
            }
            if(cnt > 0){
                recordSet.executeUpdate("update HtmlLabelIndex set id = id - 100000000  where id = " + labelid + " and indexdesc <> '" + indexdesc + "'");
            }
        }
        */
        return checkData(dataMap);
        
        
    }

    @Override
    public String getClassName() {
        return "标签缓存检测";
    }

    @Override
    public String getClassDesc() {
        return "查看是否有ID相同的标签,修复时将该数据的id修改为原id-100000000";
    }

}



